from rest_framework.permissions import BasePermission
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from.models import Doctor

class CanAddDoctorPermission(BasePermission):
    def has_permission(self, request, view):
        print(f"当前用户ID: {request.user.id}")
        print(f"当前用户名: {request.user.username}")
        try:
            content_type = ContentType.objects.get_for_model(Doctor)
            add_doctor_permission = Permission.objects.filter(
                codename='add_doctor',
                content_type=content_type
            ).first()
            if add_doctor_permission:
                print(f"权限对象: {add_doctor_permission}")
                print(f"用户权限列表: {request.user.user_permissions.all()}")
                return add_doctor_permission in request.user.user_permissions.all()
            return False
        except Exception as e:
            print(f"权限判断过程中出现异常: {e}")
            return False